var CamID = -1;
var captureCount = 0;
var DeviceStatus = false;
var isOpenCamera = false;
var rotate = 0;
var path = '';


function ShowInfo(op) {
    var obj = document.getElementById("TextArea1");
    obj.value = obj.value  + " \n" + op;
}

//初始化控件
function loadActiveX() {
    if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0)) {
        //IE浏览器加载控件
        document.getElementById("ActiveXDivOne").innerHTML = "<OBJECT id=\"axOcx\"  classid=\"clsid:D662BB12-95FC-4E4B-AD98-10AD44D6C1F0\" width=\"100%\" height=\"355px\" ></OBJECT>";      
    }else {
        //其他浏览器加载控件
        document.getElementById("ActiveXDivOne").innerHTML = "<OBJECT id=\"axOcx\" type=\"application/x-camera\"  clsid=\"{D662BB12-95FC-4E4B-AD98-10AD44D6C1F0}\"  width=\"100%\" height=\"355px\" ></OBJECT>";
    }
}


//初始化设备
function InitLoad() {

    var fileTypeObj = document.getElementById("FileType");
    fileTypeObj.selectedIndex = 0;

    var devObj;
    devObj =document.getElementById("Device");
    var devCount = axOcx.GetMyDeviceCount(); //获取设备数目
    if (devCount == 0) ShowInfo("未发现相关设备");
    if (devCount == 1){
          var objOption = document.createElement("option");
          objOption.text = "主摄像头";
          objOption.value = 0;
          devObj.options.add(objOption);
    }
    if (devCount == 2){
        var objOption = document.createElement("option");
        objOption.text = "主摄像头";
        objOption.value = 0;
        devObj.options.add(objOption);

        objOption = document.createElement("option");
        objOption.text = "辅摄像头";
        objOption.value = 1;
        devObj.options.add(objOption);
    }

    if (devCount > 0){

        var resObj = document.getElementById("Resolution");
        CamID = devObj.selectedIndex;
        var ResolutionCount = axOcx.GetResolutionCount(CamID); //获取分辨率数目
        if (ResolutionCount > 0){
            for (var i = 0; i < ResolutionCount; i++) {
                var objOption = document.createElement("option");
                objOption.text = axOcx.GetResolution(i); //获取指定索引的分辨率
                objOption.value = i;
                resObj.options.add(objOption);
            }
        }
    }
    //OpenDevice();
}

var meter1;
function countSecond() {
    alert("定时器");
    meter1 = setTimeout("countSecond()", 1000);
} 

//开启摄像头
function OpenDevice() {

    var devObj = document.getElementById("Device");
    if (devObj.options.length > 0) {

        var resObj = document.getElementById("Resolution");
        var ResolutionStr = resObj.value;
        var iRect = axOcx.OpenCamera(CamID, ResolutionStr);
        if (iRect == 0)
        {
            isOpenCamera = true;
        }
        else 
        {
            axmw_cam_ocx1.CloseCamera();
            isOpenCamera = false;
        }

    }

}

//关闭摄像头
function CloseDevice() {

    if (isOpenCamera) 
    {
        axOcx.CloseCamera()
        isOpenCamera = false;
        ShowInfo("关闭设备");
     }   
}

//抓图拍照
function Capture() {
    if (isOpenCamera == false) return;
    //captureCount = captureCount + 1;
    var obj = document.getElementById("FileType");
    //var path = "D:\\img_" + captureCount;
    path = "D:image";
    var suffix = "";
    if (obj.selectedIndex == 0) suffix = ".jpg";
    if (obj.selectedIndex == 1) suffix = ".bmp";
    if (obj.selectedIndex == 2) suffix = ".png";
    if (obj.selectedIndex == 3) suffix = ".tif";
    if (obj.selectedIndex == 4) suffix = ".gif";
    if (obj.selectedIndex == 5) suffix = ".pdf";
    path = path + suffix;
    //base64Str=axOcx.CaptureImage(path);
    //base64Str=axOcx.GetBase64FromFile(path);
    var imgobj1 = document.getElementById("img1");
    //var pic = document.getElementById("pic");
    //imgobj1.src = path;
    imgobj1.src = "data:;base64," + axOcx.CaptureImage(path); 
    imgobj1.src = "data:;base64," + axOcx.GetBase64FromFile(path); 
    //pic.value = "data:;base64," + axOcx.GetBase64FromFile(path); 
    var info = "拍照成功,图片路径:" + path;
    ShowInfo(info);
}

//切换摄像头
function ChangeDevice() {

    var resObj = document.getElementById("Resolution");
    resObj.options.length = 0;  //清空

    var devObj;
    devObj = document.getElementById("Device");

    CamID = devObj.selectedIndex;
    var ResolutionCount = axOcx.GetResolutionCount(CamID); //获取分辨率数目
    if (ResolutionCount > 0) {
        for (var i = 0; i < ResolutionCount; i++) {
            var objOption = document.createElement("option");
            objOption.text = axOcx.GetResolution(i); //获取指定索引的分辨率
            objOption.value = i;
            resObj.options.add(objOption);
        }
    }

    var ResolutionStr = resObj.options[resObj.selectedIndex].text;
    var iRect = axOcx.OpenCamera(CamID, ResolutionStr);
    if (iRect == 0) {
        isOpenCamera = true;
        ShowInfo("成功切换摄像头");
    }
    else {
        axOcx.CloseCamera();
        isOpenCamera = false;
        ShowInfo("切换摄像头失败");
    }
}

//切换分辨率
function ChangeResolution() {

    if (isOpenCamera == true)
    {
        axOcx.CloseCamera();
        isOpenCamera = false;

        var resObj = document.getElementById("Resolution");
        var ResolutionStr = resObj.options[resObj.selectedIndex].text;
        ShowInfo(ResolutionStr);
        var iRect = axOcx.OpenCamera(CamID, ResolutionStr);
        if (iRect == 0)
        {
            isOpenCamera = true;
            ShowInfo("成功切换分辨率");
        }
        else
        {
            axOcx.CloseCamera();
            isOpenCamera = false;
            ShowInfo("切换分辨率失败");
        }
    }   
}

//设置图片类型
function SetFileType() 
{
    var obj = document.getElementById("FileType");
    axOcx.SetFileType(obj.selectedIndex);
}

//设置图片颜色格式
function SetImageColorMode() 
{
    var obj = document.getElementById("ColourMode");
    axOcx.SetImageColorMode(obj.selectedIndex);
}

//设置裁剪方式
function SetCutType() {
    if (document.getElementById("Radio1").checked) {
        axOcx.SetCutType(0);
    }
    if (document.getElementById("Radio2").checked) {
        axOcx.SetCutType(1);
    }
    if (document.getElementById("Radio3").checked) {
        axOcx.SetCutType(2);
    }
}

//旋转
function SetCamRotate() {
    if (isOpenCamera == false) return;
    var obj = document.getElementById("Rotate");
    var angle = obj.options[obj.selectedIndex].text;
    axOcx.SetCamRotate(angle);
}

//Http上传测试
function HttpUploadFile() {
    var iRect = axOcx.UploadFile("http://localhost:4523/UploadFile.ashx", "C:\\test.jpg");
    if (iRect == 0) {
        ShowInfo("上传成功");
    }
    else {
        ShowInfo("上传失败");
    }
}

//放大
function ZoomIn() {
    if (isOpenCamera == false) return;
    axOcx.ZoomIn();
}

//缩小
function ZoomOut() {
    if (isOpenCamera == false) return;
    axOcx.ZoomOut();
}

//适合大小
function BestSize() {
    if (isOpenCamera == false) return;
    axOcx.BestSize();
}

//实际大小
function TrueSize() {
    if (isOpenCamera == false) return;
    axOcx.TrueSize();
}

//对焦
function ManualToFocus() {
    if (isOpenCamera == false) return;
    axOcx.ManualToFocus();
}

//设备属性
function ShowImageSettingWindow() {
    if (isOpenCamera == false) return;
    axOcx.ShowImageSettingWindow();
}


